package com.wk.springboot.spring.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Arrays;
import java.util.List;

/**
 * @User admin
 * @Date 2020/11/25 9:17
 **/
public class MyCondition implements Condition {

    private static final String BEAN_NAME = "myController";

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

        //如果spring容器中有bean:myController，则创建这个
        String[] beanDefinitionNames = context.getBeanFactory().getBeanDefinitionNames();
        List<String> list = Arrays.asList(beanDefinitionNames);
        if(list.contains(BEAN_NAME)){
            return true;
        }
        return false;
    }
}
